package cn.jly.ssm.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

/**
 * @author lanyangji
 * @description spring的上下文工具
 * @date 2020/12/11 上午 10:16
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {
    private AnnotationConfigWebApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (AnnotationConfigWebApplicationContext) applicationContext;

        // 打印容器中的bean
        final String[] beanDefinitionNames = this.applicationContext.getBeanDefinitionNames();
        System.out.println("容器中已有的bean: ");
        for (int i = 0; i < beanDefinitionNames.length; i++) {
            System.out.println("\t" + i + ", " + beanDefinitionNames[i]);
        }
    }

    public AnnotationConfigWebApplicationContext getApplicationContext() {
        return this.applicationContext;
    }
}
